package com.ruoyi.web.controller.system;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSON;
import com.github.stuxuhai.jpinyin.PinyinException;
import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.DrawElementsTool;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.*;
import com.ruoyi.common.DrawElementsFactory;
import com.ruoyi.system.service.IBmPaperService;
import jdk.nashorn.internal.parser.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IBmWordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2023-04-01
 */
@RestController
@RequestMapping("/system/word")
public class BmWordController extends BaseController
{
    @Autowired
    private IBmWordService bmWordService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private IBmPaperService iBmPaperService;

    /**
     * 查询【请填写功能名称】列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BmWord bmWord) throws PinyinException {
        //startPage();
        List<BmWord> list = bmWordService.selectBmWordList(bmWord);
        BmWord bmWord1 =new BmWord();
        for(BmWord item:list)
        {
            String pinyinString = PinyinHelper.convertToPinyinString(item.getWordName(), " ", PinyinFormat.WITH_TONE_MARK);
            if(StringUtils.isEmpty(item.getWordPinyin()))
            {
                bmWord1 = new BmWord();
                bmWord1.setId(item.getId());
                bmWord1.setWordPinyin(pinyinString);
                bmWordService.updateBmWord(bmWord1);
            }

            item.setWordPinyin(pinyinString);

        }
        return getDataTable(list);
    }

    @PostMapping("/getWordsList")
    public AjaxResult getWordsList(@RequestBody WordSearchParams wordSearchParams)
    {
        AjaxResult ajaxResult = AjaxResult.success();
        WordBean wordsList = bmWordService.getWordsList(wordSearchParams);
        ajaxResult.put("data",wordsList);
        return ajaxResult;
    }


    @PostMapping("/getPDF")
    public AjaxResult getPDF()
    {
        AjaxResult ajaxResult = AjaxResult.success();
        Paper exam = new Paper();
        exam.setExamType(PaperType.examAndAnswer);
        exam.setTitle("二年级下第pinyinList二单元听写试卷");
        List<List<String>> pinyinList = new ArrayList<>();
        List<List<String>> wordsList = new ArrayList<>();
        pinyinList.add(new ArrayList<String>(Arrays.asList(new String[]{"nǐ"})));
        pinyinList.add(new ArrayList<String>(Arrays.asList(new String[]{"nǐ"})));
        pinyinList.add(new ArrayList<String>(Arrays.asList(new String[]{"nǐ"})));
        pinyinList.add(new ArrayList<String>(Arrays.asList(new String[]{"nǐ"})));
        pinyinList.add(new ArrayList<String>(Arrays.asList(new String[]{"nǐ"})));
        pinyinList.add(new ArrayList<String>(Arrays.asList(new String[]{"nǐ"})));
        pinyinList.add(new ArrayList<String>(Arrays.asList(new String[]{"nǐ"})));
        pinyinList.add(new ArrayList<String>(Arrays.asList(new String[]{"nǐ"})));
        pinyinList.add(new ArrayList<String>(Arrays.asList(new String[]{"nǐ"})));
        pinyinList.add(new ArrayList<String>(Arrays.asList(new String[]{"nǐ"})));
        pinyinList.add(new ArrayList<String>(Arrays.asList(new String[]{"nǐ"})));
        pinyinList.add(new ArrayList<String>(Arrays.asList(new String[]{"nǐ"})));
        pinyinList.add(new ArrayList<String>(Arrays.asList(new String[]{"nǐ"})));
        pinyinList.add(new ArrayList<String>(Arrays.asList(new String[]{"nǐ"})));
        pinyinList.add(new ArrayList<String>(Arrays.asList(new String[]{"nǐ"})));
        pinyinList.add(new ArrayList<String>(Arrays.asList(new String[]{"nǐ"})));
        pinyinList.add(new ArrayList<String>(Arrays.asList(new String[]{"nǐ"})));
        pinyinList.add(new ArrayList<String>(Arrays.asList(new String[]{"nǐ"})));
        pinyinList.add(new ArrayList<String>(Arrays.asList(new String[]{"nǐ"})));
        pinyinList.add(new ArrayList<String>(Arrays.asList(new String[]{"nǐ"})));

        exam.setPinyinList(pinyinList);

        wordsList.add(new ArrayList<String>(Arrays.asList(new String[]{"你"})));
        wordsList.add(new ArrayList<String>(Arrays.asList(new String[]{"你"})));
        wordsList.add(new ArrayList<String>(Arrays.asList(new String[]{"你"})));
        wordsList.add(new ArrayList<String>(Arrays.asList(new String[]{"你"})));
        wordsList.add(new ArrayList<String>(Arrays.asList(new String[]{"你"})));
        wordsList.add(new ArrayList<String>(Arrays.asList(new String[]{"你"})));
        wordsList.add(new ArrayList<String>(Arrays.asList(new String[]{"你"})));
        wordsList.add(new ArrayList<String>(Arrays.asList(new String[]{"你"})));
        wordsList.add(new ArrayList<String>(Arrays.asList(new String[]{"你"})));
        wordsList.add(new ArrayList<String>(Arrays.asList(new String[]{"你"})));
        wordsList.add(new ArrayList<String>(Arrays.asList(new String[]{"你"})));
        wordsList.add(new ArrayList<String>(Arrays.asList(new String[]{"你"})));
        wordsList.add(new ArrayList<String>(Arrays.asList(new String[]{"你"})));
        wordsList.add(new ArrayList<String>(Arrays.asList(new String[]{"你"})));
        wordsList.add(new ArrayList<String>(Arrays.asList(new String[]{"你"})));
        wordsList.add(new ArrayList<String>(Arrays.asList(new String[]{"你"})));
        wordsList.add(new ArrayList<String>(Arrays.asList(new String[]{"你"})));
        wordsList.add(new ArrayList<String>(Arrays.asList(new String[]{"你"})));
        wordsList.add(new ArrayList<String>(Arrays.asList(new String[]{"你"})));
        wordsList.add(new ArrayList<String>(Arrays.asList(new String[]{"你"})));


        WordBean wordBean = new WordBean();
        wordBean.setWordList(wordsList);
        wordBean.setPinyinList(pinyinList);
        String json = JSON.toJSONString(wordBean);
        exam.setWordList(wordsList);
        DrawElementsFactory drawElements = new DrawElementsFactory(exam).createDrawElements();
        long milliseconds = System.currentTimeMillis();
        new DrawElementsTool(drawElements).doDrawElements().SavePdfFile("1.pdf");
        long milliseconds1 = System.currentTimeMillis();
        System.out.println(milliseconds1 - milliseconds);
        ajaxResult.put("data",wordsList);
        return ajaxResult;
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BmWord bmWord)
    {
        List<BmWord> list = bmWordService.selectBmWordList(bmWord);
        ExcelUtil<BmWord> util = new ExcelUtil<BmWord>(BmWord.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bmWordService.selectBmWordById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BmWord bmWord)
    {
        return toAjax(bmWordService.insertBmWord(bmWord));
    }

    /**
     * 修改【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BmWord bmWord)
    {
        return toAjax(bmWordService.updateBmWord(bmWord));
    }

    /**
     * 删除【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bmWordService.deleteBmWordByIds(ids));
    }
}
